package com.sp2p.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.shove.data.DataException;
import com.shove.data.DataSet;
import com.shove.util.BeanMapUtils;
import com.sp2p.database.Dao;
import com.sp2p.database.Dao.Tables;
import com.sp2p.database.Dao.Tables.t_workauth;
import com.sp2p.util.DBReflectUtil;

public class WorkAuthDao {

	/**
	 * 添加记录
	 * @param conn
	 * @param paramMap
	 * @return
	 * @throws SQLException
	 */
	public Long addWorkAuth(Connection conn ,Map<String,String> paramMap) throws SQLException{
		Dao.Tables.t_workauth  table = new Dao().new Tables().new t_workauth();
		
		table.workYear.setValue(paramMap.get("workYear"));
		table.workCity.setValue(paramMap.get("workCity"));
		table.workPro.setValue(paramMap.get("workPro"));
		table.monthlyIncome.setValue(paramMap.get("monthlyIncome"));
		table.auditStatus.setValue(paramMap.get("auditStatus"));
		table.userId.setValue(paramMap.get("USERID"));
		table.job.setValue(paramMap.get("job"));
		
		return table.insert(conn);
		
	}
	
	public Long updateWorkAuth(Connection conn ,long id,Map<String,String> paramMap) throws SQLException{
		Dao.Tables.t_workauth  table = new Dao().new Tables().new t_workauth();
		DBReflectUtil.mapToTableValue(table, paramMap);
		return table.update(conn,"id="+id);
		
	}
	
	public Map<String, String> queryFamilyById(Connection conn, Long id) throws DataException, SQLException {
		Dao.Tables.t_workauth tw = new Dao().new Tables().new t_workauth();
		DataSet ds = tw.open(conn, " * ", "  userId=" + id, "", -1, -1);
		return BeanMapUtils.dataSetToMap(ds);
	}
	
//	public Map<String,String> getWorkAuth(Connection conn ,long userId) throws SQLException, DataException{
//		Dao.Views.V_T_WORKAUTH_LINKMAN work = new Dao().new Views().new V_T_WORKAUTH_LINKMAN();
//		DataSet ds = work.open(conn, "*", "\"userId\"="+userId, "", -1, -1);
//		return BeanMapUtils.dataSetToMap(ds);
//		
//	}
	
	public long updateWorkAuth(Connection conn, Long id, String familyName, String familyRelation, String familyAdd, String familyTel,
			String familyIdNo, String otherAdd, String otherIdNo, String otherName, String otherRelation, String otherTel, Long userid)
			throws SQLException {
		Dao.Tables.t_workauth tw = new Dao().new Tables().new t_workauth();
		if (!StringUtils.isBlank(familyName))
			tw.directedName.setValue(familyName);
		if (!StringUtils.isBlank(familyRelation))
			tw.directedRelation.setValue(familyRelation);
		if (!StringUtils.isBlank(familyTel))
			tw.directedTel.setValue(familyTel);
		if (!StringUtils.isBlank(familyAdd))
			tw.directedAddress.setValue(familyAdd);
		if (!StringUtils.isBlank(familyIdNo))
			tw.directedIdNo.setValue(familyIdNo);

		if (!StringUtils.isBlank(otherName))
			tw.otherName.setValue(otherName);
		if (!StringUtils.isBlank(otherRelation))
			tw.otherRelation.setValue(otherRelation);
		if (!StringUtils.isBlank(otherTel))
			tw.otherTel.setValue(otherTel);
		if (!StringUtils.isBlank(otherAdd))
			tw.otherAddress.setValue(otherAdd);
		if (!StringUtils.isBlank(otherIdNo))
			tw.otherIdNo.setValue(otherIdNo);
		
		//合和年无此项信息审核,直接通过
		tw.moredStatus.setValue(3);
		
		return tw.update(conn, " userId=" + userid);
	}
}
